from djcontrol import *

from Tkinter import *
import djcontrolutil


class DjLabel(DjControl):
	"""
	Label widget.
	
	Properties:
	
	*text* - String. Current contents of the label. Use '&' to specify a character to be underlined. For example, if the text is set to '&hello', the h will be underlined.
	*bgcolor* - Background color of the label. RGB triplet, such as (255,0,0) for pure red.
	*color* - Foreground color of the label. RGB triplet.
	
	*enabled* - If False, the label is grayed out.
	*width* - Set width of input, where one unit is the width of a char. If set to 0, width is based on contents (the default behavior).
	*height* - Set height of input, by the height of a line of text. If set to 0, width is based on contents (the default behavior).
	
	*font* - Set font of input. Standard tkinter font format, such as ('Times New Roman', 12, 'normal')
	*wrapwidth* - Set when the text should be wrapped into multiple lines, in screen units. Default is 0 (no wrapping).
	*borderwidth* - An integer, width of border in pixels.
	*alignment* - Set alignment of text when there are multiple lines. One of 'left','right','center'.
	*borderstyle* - Border style. One of 'sunken','raised','groove','ridge','flat'. Default is 'sunken'.
	*anchor* - Set where in the button the text (or image) should be located. Use one of n, ne, e, se, s, sw, w, nw, or center. Default is center.
	
	"""
	
	# Note that only names beginning with '_', or the special case basecontrol, should be used.
	_autoProperties = None
	basecontrol = 0
	
	def __init__(self, parent, strText='', **options):
		parent = djcontrolutil.getbasecontrol(parent)
		self.basecontrol = Label(parent)
		
		self._autoProperties = { 
		# Property name,  Tkinter property name, SET wrapper, GET wrapper
		'bgcolor': ('background',djcontrolutil.colorToString,djcontrolutil.stringToColor),
		'color': ('foreground',djcontrolutil.colorToString,djcontrolutil.stringToColor),
		'enabled':('state', _boolToState, _stateToBool),
		'width': ('width',None,None),
		'height': ('height',None,None),
		'font': ('font',None,None),
		'wrapwidth': ('wraplength',None,None),
		'borderwidth': ('borderwidth',None,None),
		'alignment': ('justify',djcontrolutil.stringToJustify,djcontrolutil.justifyToString),
		'borderstyle': ('relief', djcontrolutil.stringToRelief,djcontrolutil.reliefToString),
		'anchor': ('anchor',djcontrolutil.stringToAnchor,djcontrolutil.anchorToString),
		}
		if strText!='': self.text = strText
		self.set_many(**options)
	
	
	def clear(self):
		"""Delete all text."""
		self.basecontrol.config(text='')
		self.basecontrol.update_idletasks()
	
	
	_customProperties = ('text')
	def _lookupCustomProperties(self, bSet, strAttribute, value=None):
		if strAttribute=='text':
			if bSet:
				text, nIndex = djcontrolutil.stringToUnderlinePosition(value) # If given a string '&hello', underline the h.
				self.basecontrol.config(text=text,underline=nIndex)
				self.basecontrol.update_idletasks()
			else:
				return djcontrolutil.underlinePositionToString(self.basecontrol['text'], self.basecontrol['underline'])
				
		

def _boolToState(b):
	if b: return NORMAL
	else: return DISABLED
def _stateToBool(state):
	if state==DISABLED: return False
	else: return True

